package com.aw.puzzle.dropboxdiet;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * User: gmc
 * Date: 29/01/11
 */
public class ActivityGroup {
    private List<Activity> activities = new ArrayList();
    private int[] combination;
    private int value = 0;

    public ActivityGroup() {
    }

    public ActivityGroup(int[] combination) {
        this.combination = combination;
    }

    public void addActivity(Activity activity) {
        activities.add(activity);
        value += activity.getAbsValue();
    }

    public int getValue() {
        return value;
    }

    public int size() {
        return activities.size();
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (Activity activity : activities) {
            sb.append(activity.getName());
            sb.append(" ");
            sb.append(activity.getCalorieValue());
            sb.append("\n");
        }
        return sb.toString();
    }

    private void setActivities(List<Activity> activities) {
        this.activities = activities;
    }

    public List<Activity> getActivities() {
        return activities;
    }

    public static ActivityGroup union(ActivityGroup activityGroup1, ActivityGroup activityGroup2) {
        List allActivities = new ArrayList();
        allActivities.addAll(activityGroup1.getActivities());
        allActivities.addAll(activityGroup2.getActivities());
        ActivityGroup activityGroup = new ActivityGroup();
        activityGroup.setActivities(allActivities);
        return activityGroup;
    }

    public int[] getCombination() {
        return combination;
    }

    public void setCombination(int[] combination) {
        this.combination = combination;
    }

    public String getStrOfActivitiesSorted() {
        List<Activity> sortedActivities = new ArrayList(activities);
        Collections.sort(sortedActivities,new Comparator<Activity>(){
            public int compare(Activity o1, Activity o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        StringBuilder sb = new StringBuilder();
        for (Activity activity : sortedActivities) {
            sb.append(activity.getName());
            sb.append("\n");
        }
        return sb.toString();
    }
}
